// 深拷贝
export function deepClone(source) {
  let target;
  if (typeof source === 'object') {
    target = Array.isArray(source) ? [] : {}
    for (let key in source) {
      if (source.hasOwnProperty(key)) {
        if (typeof source[key] !== 'object') {
          target[key] = source[key]
        } else {
          target[key] = deepClone(source[key])
        }
      }
    }
  } else {
    target = source
  }
  return target
}

export function parseTime(time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = cFormat || 'y-m-d h:i:s'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    // if (('' + time).length === 10) time = parseInt(time) * 1000
    date = new Date(time)
    // eslint-disable-next-line eqeqeq
    if (date == 'Invalid Date') return time
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/(y|m|d|h|i|s|a)/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value]
    }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}


export function value2Level(tree, value, childName = 'children') {
  const level = []
    ; (function value2Level(data, value, parent) {
      Array.isArray(data) &&
        data.forEach((item, index) => {
          if (item.value !== value) {
            item[childName] && value2Level(item[childName], value, [data, index])
          } else {
            parent && value2Level(tree, parent[0][parent[1]].value)
            level.push(item.label)
          }
        })
    })(tree, value)
  return level
}

export function treeFather(tree, value, childName = 'children') {
  const level = []
    ; (function value2Level(data, value, parent) {
      Array.isArray(data) &&
        data.forEach((item, index) => {
          if (item.value !== value) {
            item[childName] && value2Level(item[childName], value, [data, index])
          } else {
            parent && value2Level(tree, parent[0][parent[1]].value)
            level.push(item.value)
          }
        })
    })(tree, value)
  return level
}

// yyyyMMddHHmmss时间格式转化为yyyy-MM-dd HH:mm:ss
  export function formatDateTime(dateTime,timeNumber){
  if (timeNumber==4){
    return  dateTime.replace(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})$/, "$1-$2-$3 $4:$5")
  } else {
    return  dateTime.replace(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/, "$1-$2-$3 $4:$5:$6")
  }
}

// 格式化此刻日期为yyyyMMddHHmmss
export function pad2(n) { return n < 10 ? '0' + n : n }

export function generateTimeReqestNumber() {
    var date = new Date();
    return date.getFullYear().toString() + pad2(date.getMonth() + 1) + pad2(date.getDate()) + pad2(date.getHours()) + pad2(date.getMinutes()); // + pad2(date.getSeconds()
}

export function generateTimeReqestNumber2() {
    var date = new Date();
    return date.getFullYear().toString() + pad2(date.getMonth() + 1) + pad2(date.getDate()) + pad2(date.getHours()) + pad2(date.getMinutes()) + pad2(date.getSeconds()); 
}

 